import { ChangePwdBody, CrudBack, R, RegisterParams, SysConfig, SysMenuTree, SysRole, SysUser, SysUserOnline } from '@/interface/user';
import request from '../utils/request'
import { AxiosResponse } from 'axios';
import { Page } from '@/interface/log';
import { SysMenu } from '@/interface/menu';
// 响应接口
export interface GetCaptchaImageRes {
  /* */
  code: number;

  /* */
  msg: string;

  /* */
  data: Record<string, any>;
}

/** 
 * 获取验证码
 * @returns
 */
export function getCaptchaImage(): Promise<GetCaptchaImageRes> {
  return request.get(`/security/captcha/image`);
}

// 参数接口
export interface LoginParams {
  /* */
  username: string;

  /* */
  password: string;

  /* */
  captchaCode?: string;

  /* */
  uuidKey?: string;
  remember?: boolean
}

// 响应接口
export interface LoginRes<T> {
  /* */
  code: number;

  /* */
  msg: string;

  /* */
  data: T;
}

/** 
 * 登录
 * @param {object} params LoginBody
 * @param {string} params.username 
 * @param {string} params.password 
 * @param {string} params.captchaCode 
 * @param {string} params.uuidKey 
 * @returns
 */
export function login(params: LoginParams): Promise<AxiosResponse<LoginRes<any>>> {
  return request.post(`/security/auth/login`, params);
}

// 响应接口
export interface InfoRes {
  /* */
  code: number;

  /* */
  msg: string;

  /* */
  data: SysUser;
}

/** 
 * 获取用户信息
 * @returns
 */
export function info(): Promise<AxiosResponse<InfoRes>> {
  return request.get(`/sys/user/info`, {
    headers: {
      isToken: true
    }
  });
}

// 响应接口
export interface LogoutRes {
  /* */
  code: number;

  /* */
  msg: string;

  /* */
  data: any;
}

/** 
 * 退出登录
 * @returns
 */
export function logout(): Promise<AxiosResponse<LogoutRes>> {
  return request.post(`/security/auth/logout`, null, {
    headers: {
      isToken: true
    }
  });
}

// 响应接口
export interface GetMenuTreeRes<T> {
  /* */
  code: number;

  /* */
  msg: string;

  /* */
  data: T;
}

/** 
 * 获取(树形)
 * @returns
 */
export function getMenuTree(): Promise<AxiosResponse<GetMenuTreeRes<SysMenuTree[]>>> {
  return request.get(`/sys/menu/tree`, {
    headers: {
      isToken: true
    }
  });
}

// 响应接口
export interface GetUserPageRes<T> {
  /* */
  code: number;

  /* */
  msg: string;

  /* */
  data: T;
}

/** 
 * 查询用户列表(page)
 * @param {string} current 
  * @param {string} pageSize 
  * @returns
 */
export function getUserPage(current: number, pageSize: number, params: string = ''): Promise<any> {
  return request.get(`/sys/user/page?current=${current}&pageSize=${pageSize}&params=${params}`, {
    headers: {
      isToken: true
    }
  });
}

// 响应接口
export interface DeleteRes {
  /* */
  code: number;

  /* */
  msg: string;

  /* */
  data: any;
}

/** 
 * 删除用户(需要权限)
 * @param {string} ids 
  * @returns
 */
export function deleteUser(ids: string): Promise<DeleteRes> {
  return request.post(`/sys/user/delete/${ids}`, null, {
    headers: {
      isToken: true
    }
  });
}

// 响应接口
export interface AdminUpdateRes {
  /* */
  code: number;

  /* */
  msg: string;

  /* */
  data: any;
}

/** 
 * 更新用户信息(admin)
 * @param {object} params SysUser
 * @returns
 */
export function adminUpdate(params: SysUser): Promise<AdminUpdateRes> {
  return request.post(`/sys/user/admin/update`, params, {
    headers: {
      isToken: true
    }
  });
}

// 响应接口
export interface GetAdminMenuTreeRes {
  /* */
  code: number;

  /* */
  msg: string;

  /* */
  data: SysMenuTree[];
}

/** 
 * 菜单管理(树形)
 * @returns
 */
export function getAdminMenuTree(): Promise<AxiosResponse<GetAdminMenuTreeRes>> {
  return request.get(`/sys/menu/admin/tree`, {
    headers: {
      isToken: true
    }
  });
}

// 响应接口
export interface UpdateMenuRes {
  /* */
  code: number;

  /* */
  msg: string;

  /* */
  data: any;
}

/** 
 * 修改
 * @param {object} params SysMenu
 * @returns
 */
export function updateMenu(params: SysMenuTree): Promise<AxiosResponse<UpdateMenuRes>> {
  return request.post(`/sys/menu/update`, params, {
    headers: {
      isToken: true
    }
  });
}

// 响应接口
export interface AllRole {
  /* */
  code: number;

  /* */
  msg: string;

  /* */
  data: Page<SysRole[]>;
}

/** 
 * 获取全部
 * @returns
 */
export function getAllRole(current: number, pageSize: number, params: string = ''): Promise<AxiosResponse<AllRole>> {
  return request.get(`/sys/role/all?current=${current}&pageSize=${pageSize}&params=${params}`, {
    headers: {
      isToken: true
    }
  });
}

// 响应接口
export interface UpdateRoleApi {
  /* */
  code: number;

  /* */
  msg: string;

  /* */
  data: any;
}

/** 
 * 更新
 * @param {object} params SysRole
 */
export function updateRole(params: SysRole): Promise<AxiosResponse<UpdateRoleApi>> {
  return request.post(`/sys/role/update`, params, {
    headers: {
      isToken: true
    }
  });
}

// 响应接口
export interface DeleteRolesApi {
  /* */
  code: number;

  /* */
  msg: string;

  /* */
  data: any;
}

/** 
 * 删除
 * @param {string} ids 
  * @returns
 */
export function deleteRoles(ids: string): Promise<AxiosResponse<DeleteRolesApi>> {
  return request.post(`/sys/role/delete/${ids}`, null, {
    headers: {
      isToken: true
    }
  });
}

// 响应接口
export interface RoleUsersRes {
  /* */
  code: number;

  /* */
  msg: string;

  /* */
  data: any;
}

/** 
 * 角色对应的用户
 * @returns
 */
export function roleUsers(): Promise<AxiosResponse<RoleUsersRes>> {
  return request.get(`/sys/role/roleUsers`, {
    headers: {
      isToken: true
    }
  });
}

// 响应接口
export interface RemoveRoleUsersRes {
  /* */
  code: number;

  /* */
  msg: string;

  /* */
  data: any;
}

/** 
 * 移出角色组
 * @param {string} userId 
  * @param {string} roleId 
  * @returns
 */
export function removeRoleUsersApi(userId: number | undefined, roleId: number | undefined): Promise<AxiosResponse<RemoveRoleUsersRes>> {
  return request.post(`/sys/role/removeRoleUsers/${userId}/${roleId}`, null, {
    headers: {
      isToken: true
    }
  });
}

// 响应接口
export interface GetUserNameLikeRes {
  /* */
  code: number;

  /* */
  msg: string;

  /* */
  data: SysUser[];
}

/** 
 * 用户名(like)
 * @param {string} userName 
  * @returns
 */
export function getUserNameLike(userName: string): Promise<AxiosResponse<GetUserNameLikeRes>> {
  return request.get(`/sys/user/like?userName=${userName}`, {
    headers: {
      isToken: true
    }
  });
}

// 响应接口
export interface AddRoleUsersRes {
  /* */
  code: number;

  /* */
  msg: string;

  /* */
  data: any;
}

/** 
 * 分配角色
 * @param {string} userIds 
  * @param {string} roleId 
  * @returns
 */
export function addRoleUsersApi(userIds: string, roleId: number): Promise<AxiosResponse<AddRoleUsersRes>> {
  return request.post(`/sys/role/addRoleUsers/${userIds}/${roleId}`, null, {
    headers: {
      isToken: true
    }
  });
}

// 响应接口
export interface UserOnlineRes {
  /* */
  code: number;

  /* */
  msg: string;

  /* */
  data: SysUserOnline[];
}

/** 
 * 查询全部
 * @returns
 */
export function getUserOnlineApi(userName: string): Promise<AxiosResponse<UserOnlineRes>> {
  return request.get(`/sys/online/all?userName=${userName}`, {
    headers: {
      isToken: true
    }
  });
}

// 响应接口
export interface KickRes {
  /* */
  code: number;

  /* */
  msg: string;

  /* */
  data: any;
}

/** 
 * 强制下线
 * @param {string} userId 
  * @returns
 */
export function kickUserApi(userId: number): Promise<AxiosResponse<KickRes>> {
  return request.post(`/sys/online/kick/${userId}`, null, {
    headers: {
      isToken: true
    }
  });
}

// 响应接口
export interface GetMenuListRes<T> {
  /* */
  code: number;

  /* */
  msg: string;

  /* */
  data: T;
}

/** 
 * 获取(列表)
 * @returns
 */
export function getMenuList(): Promise<AxiosResponse<GetMenuListRes<SysMenu[]>>> {
  return request.get(`/sys/menu/all`, {
    headers: {
      isToken: true
    }
  });
}

// 参数接口

// 响应接口
export interface RoleAddAuthRes {
  /* */
  code: number;

  /* */
  msg: string;

  /* */
  data: any;
}

/** 
 * 角色授权
 * @param {string} roleId 
  * @param {array} params string[]
 * @returns
 */
export function roleAddAuth(roleId: number, params: string[]): Promise<AxiosResponse<RoleAddAuthRes>> {
  return request.post(`/sys/role/add/auth/${roleId}`, params, {
    headers: {
      isToken: true
    }
  });
}

// 响应接口
export interface RoleAuthAllRes {
  /* */
  code: number;

  /* */
  msg: string;

  /* */
  data: string[];
}

/** 
 * 角色拥有的权限
 * @param {string} roleId 
  * @returns
 */
export function roleAuthAll(roleId: number): Promise<AxiosResponse<RoleAuthAllRes>> {
  return request.get(`/sys/role/auth/all/${roleId}`, {
    headers: {
      isToken: true
    }
  });
}

// 响应接口
export interface RegisterRes {
  /* */
  code: number;

  /* */
  msg: string;

  /* */
  data: any;
}

/** 
 * 注册
 * @param {object} params LoginBody
 * @param {string} params.username 
 * @param {string} params.password 
 * @param {string} params.captchaCode 
 * @param {string} params.uuidKey 
 * @returns
 */
export function registerUserApi(params: RegisterParams): Promise<AxiosResponse<RegisterRes>> {
  return request.post(`/security/auth/register`, params);
}

// 响应接口
export interface AddRoleRes {
  /* */
  code: number;

  /* */
  msg: string;

  /* */
  data: Record<string, unknown>;
}

/** 
 * 新增
 * @param {object} params SysRole
 * @returns
 */
export function addRoleApi(params: SysRole): Promise<AxiosResponse<AddRoleRes>> {
  return request.post(`/sys/role/add`, params, {
    headers: {
      isToken: true
    }
  });
}

/** 
 * 增加
 * @param {object} params SysMenu
 * @returns
 */
export function addMenu(params: SysMenu): Promise<AxiosResponse<CrudBack>> {
  return request.post(`/sys/menu/add`, params, {
    headers: {
      isToken: true
    }
  });
}

/** 
 * 删除
 * @param {string} ids 
  * @returns
 */
export function deleteMenuApi(ids: string): Promise<AxiosResponse<CrudBack>> {
  return request.post(`/sys/menu/delete/${ids}`, null, {
    headers: {
      isToken: true
    }
  });
}

/** 
 * 获取系统配置(self)
 * @returns
 */
export function getSelfConfigApi(): Promise<AxiosResponse<R<SysConfig>>> {
  return request.get(`/sys/config`, {
    headers: {
      isToken: true
    }
  });
}

/** 
 * 修改配置
 * @param {string} sysConfig 
  * @returns
 */
export function updateConfigApi(sysConfig: SysConfig): Promise<AxiosResponse<CrudBack>> {
  return request.post(`/sys/config/update?sysConfig`, sysConfig, {
    headers: {
      isToken: true
    }
  });
}

/** 
 * 重置配置
 * @returns
 */
export function resetConfigApi(): Promise<AxiosResponse<CrudBack>> {
  return request.post(`/sys/config/reset`, null, {
    headers: {
      isToken: true
    }
  });
}

export function updateUserInfoApi(params: SysUser): Promise<AxiosResponse<R<any>>> {
  return request.post(`/sys/user/update`, params, {
    headers: {
      isToken: true
    }
  });
}

/** 
 * 刷新token
 * @returns
 */
export function refreshTokenApi(): Promise<AxiosResponse<R<any>>> {
  return request.post(`/security/auth/refresh`, null, {
    headers: {
      isToken: true
    }
  });
}

/** 
 * 修改密码(self)
 */
export function changePwdApi(params: ChangePwdBody): Promise<AxiosResponse<R<any>>> {
  return request.post(`/security/auth/changePwd`, params, {
    headers: {
      isToken: true
    }
  });
}